﻿///<reference path="_moduleReferences.ts" />

module ScrambledEggs.Modules {
    export class Job implements IModule {

        private _onUpdate: () => void;
        private _youngBasics = $("#youngbasics");
        private _jobs = $("#jobs");
        private _gotojob = $("#gotojob");
        private _activeJob: PartTimeJob = null;
        private _jobTickTimeout: number = null;

        constructor() {
        }

        initialize(gameState: GameStateProvider, onUpdate: () => void ) {
            var me = this;
            me._onUpdate = onUpdate;

            me._youngBasics.find("button.findjob").click(function () {
                me._youngBasics.hide();
                me._jobs.show();

                me._jobs.find(".other").click();
            });

            me._jobs.find(".cancel").click(function () {
                me._youngBasics.show();
                me._jobs.hide();
            });
            me._jobs.find(".other").click(function () {
                var jobs = me._jobs.find(".job");
                var job: JQuery = null;
                while(job == null || job.is(":visible"))
                    job = $(jobs.get(Utility.Number.Random(0, jobs.length)));
                me._activeJob = PartTimeJob[job.attr("data-job")];
                jobs.not(job.show()).hide();
            });
            me._jobs.find(".apply").click(function () {
                gameState.State.Message = "Let's see if you're up for the job...";
                me._onUpdate();
                me._youngBasics.hide();
                var confirmJob = function () {
                    me._youngBasics.show();
                    me._jobs.hide();
                    gameState.State.PartTimeJob = me._activeJob;
                    me._onUpdate();
                };
                switch (me._activeJob) {
                    case PartTimeJob.Banker:
                        Utility.General.YesNoBox("Are you sure you want to become a banker?", function () {
                            Utility.General.YesNoBox("Are you very sure? Banking is boring...", function () {
                                Utility.General.YesNoBox("So you really want to be a banker?", function () {
                                    Utility.General.YesNoBox("Are you just clicking that button?", $.noop, function () {
                                        Utility.General.OptionBox("Last chance to change your mind.",
                                            { Content: "I've changed my mind", Action: $.noop },
                                            { Content: "Let me be a banker!", Action: confirmJob }
                                        );
                                    });
                                }, $.noop);
                            }, $.noop);
                        }, $.noop);
                        break;
                    case PartTimeJob.Cleaning:
                        Utility.General.YesNoBox("Are you sure you want to clean - I mean, tame - dragons?", function () {
                            Utility.General.YesNoBox("And you do have experience with cleaning?", function () {
                                Utility.General.YesNoBox("Are you the sort of person that doens't mind being tricked?", function () {
                                    Utility.General.YesNoBox("If this job had nothing to do with dragons, would be upset?", $.noop, function () {
                                        Utility.General.OptionBox("Will you accept the job offer?",
                                            { Content: "I think I'll pass", Action: $.noop },
                                            { Content: "Yes, I see nothing fishy with this offer and accept it!", Action: confirmJob }
                                        );
                                    });
                                }, $.noop);
                            }, $.noop);
                        }, $.noop);
                        break;
                    case PartTimeJob.Manager:
                        Utility.General.YesNoBox("Do you think you'd make a great manager?", function () {
                            Utility.General.YesNoBox("Do you feel superior to most people in most ways?", function () {
                                Utility.General.YesNoBox("Are you sure you want to make heaps of money by doing nothin all day?", function () {
                                    Utility.General.YesNoBox("If I asked you for a raise, would you give me one?", $.noop, function () {
                                        Utility.General.OptionBox("I feel like you're overqualified, but if you want, you've got the job!",
                                            { Content: "I don't think managing is my thing after all.", Action: $.noop },
                                            { Content: "Hand me the whip. I can start tomorrow!", Action: confirmJob }
                                        );
                                    });
                                }, $.noop);
                            }, $.noop);
                        }, $.noop);
                        break;
                    case PartTimeJob.Research:
                        Utility.General.YesNoBox("Are you generally interested in science?", function () {
                            Utility.General.YesNoBox("Are you, generally speaking, an intelligent person?", function () {
                                Utility.General.YesNoBox("Are you resistant to all kinds of pain and discomfort?", function () {
                                    Utility.General.YesNoBox("Would you agree that evolution is just a theory?", $.noop, function () {
                                        Utility.General.OptionBox("Do you really feel like you're up for this, then?",
                                            { Content: "On second thought, I don't very much enjoy pain.", Action: $.noop },
                                            { Content: "No pain, no gain! Let's do some science!", Action: confirmJob }
                                        );
                                    });
                                }, $.noop);
                            }, $.noop);
                        }, $.noop);
                        break;
                    case PartTimeJob.Shoemaker:
                        Utility.General.YesNoBox("Do you have any experience with leatherworking?", function () {
                            Utility.General.YesNoBox("Do you desire to make the best shoe ever made?", function () {
                                Utility.General.YesNoBox("Have you always dreamt of exploring the universe wearing the ultimate footwear??", function () {
                                    Utility.General.YesNoBox("Are you terrible at keeping secrets?", $.noop, function () {
                                        Utility.General.OptionBox("Do you accept this job as a shoemaker?",
                                            { Content: "Actually, shoes are boring.", Action: $.noop },
                                            { Content: "It's time to make some shoes!", Action: confirmJob }
                                        );
                                    });
                                }, $.noop);
                            }, $.noop);
                        }, $.noop);
                        break;
                }
            });

            me._youngBasics.find("button.gotojob").click(function () {
                switch (gameState.State.PartTimeJob) {
                    case PartTimeJob.Banker:
                        gameState.State.JobCooldown = 160; // 2m40s => 8h | 1tick = 3m
                        gameState.State.Message = "You stare at your watch, wishing you could go home soon.";
                        break;
                    case PartTimeJob.Shoemaker:
                        gameState.State.Message = "You set out to find the materials required for shoe making.";
                        $.get(ScrambledEggs.Settings.Path.ShoemakerLevels[gameState.State.JobLevel], {}, (levelData) => {
                            gameState.State.JobCooldown = 1;
                            me._onUpdate();
                            var adventure: Adventure;
                            var glyphs: { [char: string]: Glyph; } = {};

                            var playerGlyph = new Glyph("ô", () => { return true; }, gameState.State.Hat, "rgb(0, 200, 0)");
                            glyphs["*"] = new Glyph("*", () => { return false; }, "*", "rgb(0, 0, 200)");
                            glyphs["#"] = new Glyph("#", () => { return true; }, "#", "rgb(200, 0, 0)", function () { console.log("glyphs happen!"); });

                            adventure = new Adventure(levelData, glyphs, playerGlyph, me._gotojob.find(".shoemaker").find("canvas").get(0));
                            adventure.Start();
                        }, "text");
                        break;
                    default: Utility.General.InfoBox("Job is not yet implemented")
                        break;
                }
                me._onUpdate();
            });
        }

        update(gameState: GameStateProvider, triggerSource: IModule): void {
            var me = this;
            if (gameState.State.Phase != GamePhase.YoungAdult) {
                me._youngBasics.find("button.findjob").hide();
                return;
            }
            me._youngBasics.find("button.findjob").toggle(gameState.State.PartTimeJob == PartTimeJob.None);
            me._youngBasics.find("button.gotojob").toggle(gameState.State.PartTimeJob != PartTimeJob.None);

            if (gameState.State.JobCooldown <= 0)
                me._gotojob.hide();
            else {
                me._gotojob.show();
                
                if (gameState.State.PartTimeJob == PartTimeJob.Banker && !me._jobTickTimeout) {
                    me._jobTickTimeout = setTimeout(function () {
                        if (--gameState.State.JobCooldown <= 0) {
                            gameState.State.JobCooldown = 0;
                            gameState.State.Money += Utility.Number.Random(2000, 2500);
                            gameState.State.Energy = 0;
                            gameState.State.Time -= 1;
                        }
                        me._jobTickTimeout = null;
                        me._onUpdate();
                    }, 1000);

                    me._gotojob.find(".banker").show();
                    var currentTime = 9 * 60 + (160 - gameState.State.JobCooldown) * 3;
                    var currentHour = Math.floor(currentTime / 60);
                    var currentMinute = currentTime - currentHour * 60;

                    var hourRep = currentHour < 10 ? "0" + currentHour : "" + currentHour;
                    var minuteRep = currentMinute < 10 ? "0" + currentMinute : "" + currentMinute;

                    me._gotojob.find(".banker .time").html(hourRep + " : " + minuteRep);
                }
                else if (gameState.State.PartTimeJob == PartTimeJob.Shoemaker) {
                    me._gotojob.find(".shoemaker").show();
                }
            }
        }
    }
}